<html>
<head><meta charset="utf-8"><title>size_of_val total allocated memory for value · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/size_of_val.20total.20allocated.20memory.20for.20value.html">size_of_val total allocated memory for value</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="225489931"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/size_of_val%20total%20allocated%20memory%20for%20value/near/225489931" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/size_of_val.20total.20allocated.20memory.20for.20value.html#225489931">(Feb 07 2021 at 23:24)</a>:</h4>
<p>Is there an equivalent of <code>size_of_val</code> that includes the size of allocated buffers? E.g., this:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">x</span>: <span class="nb">Option</span><span class="o">&lt;</span><span class="nb">Vec</span><span class="o">&lt;</span><span class="nb">String</span><span class="o">&gt;&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="fm">vec!</span><span class="p">[</span><span class="s">"one"</span><span class="p">.</span><span class="n">into</span><span class="p">(),</span><span class="w"> </span><span class="s">"two"</span><span class="p">.</span><span class="n">into</span><span class="p">(),</span><span class="w"> </span><span class="s">"three"</span><span class="p">.</span><span class="n">into</span><span class="p">()]);</span><span class="w"></span>
<span class="w">    </span><span class="fm">println!</span><span class="p">(</span><span class="s">"{}"</span><span class="p">,</span><span class="w"> </span><span class="n">std</span>::<span class="n">mem</span>::<span class="n">size_of_val</span><span class="p">(</span><span class="o">&amp;</span><span class="n">x</span><span class="p">));</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>prints <code>24</code>, but I would like it to print <code>96</code> (<code>24 + 3 * 24</code>), or whatever the total allocated memory size is. Is this even possible?</p>



<a name="225490155"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/size_of_val%20total%20allocated%20memory%20for%20value/near/225490155" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/size_of_val.20total.20allocated.20memory.20for.20value.html#225490155">(Feb 07 2021 at 23:30)</a>:</h4>
<p>I use a modification of the <a href="https://docs.rs/deepsize/0.2.0/deepsize/"><code>deepsize</code></a> crate for this in my project</p>



<a name="225490274"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/size_of_val%20total%20allocated%20memory%20for%20value/near/225490274" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/size_of_val.20total.20allocated.20memory.20for.20value.html#225490274">(Feb 07 2021 at 23:33)</a>:</h4>
<p>Unfortunately, all relevant data structures have to implement this trait, which means that if any data structures you use don't have an implementation in the deepsize crate, you are stuck because of the orphan rule. That's why I had to copy it into my project</p>



<a name="225490459"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/size_of_val%20total%20allocated%20memory%20for%20value/near/225490459" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/size_of_val.20total.20allocated.20memory.20for.20value.html#225490459">(Feb 07 2021 at 23:37)</a>:</h4>
<p>Also, in order to implement this on third party data structures you have to have access to all the allocations, which is a problem because of privacy. For example if you want to know how many allocations are in your hashmap, you need access to the underlying bucket array</p>



<a name="225495016"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/size_of_val%20total%20allocated%20memory%20for%20value/near/225495016" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/size_of_val.20total.20allocated.20memory.20for.20value.html#225495016">(Feb 08 2021 at 01:23)</a>:</h4>
<p>Yeah, I just want to know because I'm curious how much space my type takes up in memory (the <code>{:#?}</code> output is 800 lines long...). I wonder if having an intrinsic for this purpose would work better...</p>



<a name="225495266"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/size_of_val%20total%20allocated%20memory%20for%20value/near/225495266" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/size_of_val.20total.20allocated.20memory.20for.20value.html#225495266">(Feb 08 2021 at 01:30)</a>:</h4>
<p>I don't think there's a good way for the compiler or language to do this; the most we could do is provide a deepsize/heap_size_of/etc trait in core.</p>



<a name="225495413"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/size_of_val%20total%20allocated%20memory%20for%20value/near/225495413" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/size_of_val.20total.20allocated.20memory.20for.20value.html#225495413">(Feb 08 2021 at 01:33)</a>:</h4>
<p>Another option would be a tracking allocator; this is easiest if the goal is to count up <em>all</em> allocated memory in the program, since we don't really have scoped allocators rn</p>



<a name="225542136"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/size_of_val%20total%20allocated%20memory%20for%20value/near/225542136" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/size_of_val.20total.20allocated.20memory.20for.20value.html#225542136">(Feb 08 2021 at 13:17)</a>:</h4>
<p>I'm using the allocator path to provide localized memory benchmarks for my crate</p>



<a name="225551100"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/size_of_val%20total%20allocated%20memory%20for%20value/near/225551100" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Eh2406 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/size_of_val.20total.20allocated.20memory.20for.20value.html#225551100">(Feb 08 2021 at 14:27)</a>:</h4>
<p>I think jemalock has some useful api's but I am on windows so have never tested.<br>
<a href="https://github.com/nnethercote/dhat-rs">https://github.com/nnethercote/dhat-rs</a> is amazing, at what it does, so I'd start there.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>